package com.spzx.test1.fewu;

import java.util.concurrent.atomic.AtomicLong;



public class CostCalculator {

    // 用于存储人工成本
    private AtomicLong laborCost = new AtomicLong(0);
    // 用于存储材料成本
    private AtomicLong materialCost = new AtomicLong(0);
    // 用于存储工程基础造价
    private AtomicLong baseCost = new AtomicLong(0);
    // 用于存储工程总造价
    private AtomicLong totalCost = new AtomicLong(0);

    // 计算人工成本的线程任务
    public class LaborCostCalculator extends Thread {
        @Override
        public void run() {
            // 模拟计算人工成本，假设为 10000
            laborCost.set(10000);
            System.out.println("人工成本计算完成: " + laborCost.get());
        }
    }

    // 计算材料成本的线程任务
    public class MaterialCostCalculator extends Thread {
        @Override
        public void run() {
            // 模拟计算材料成本，假设为 5000
            materialCost.set(5000);
            System.out.println("材料成本计算完成: " + materialCost.get());
        }
    }

    // 计算工程基础造价的线程任务
    public class BaseCostCalculator extends Thread {
        @Override
        public void run() {
            // 模拟计算工程基础造价，假设为 8000
            baseCost.set(8000);
            System.out.println("工程基础造价计算完成: " + baseCost.get());
        }
    }


    // 计算总造价的线程任务
    public class TotalCostCalculator extends Thread {
        @Override
        public void run() {
            // 等待其他计算任务完成
            while (laborCost.get() == 0 || materialCost.get() == 0 || baseCost.get() == 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            // 计算总造价
            totalCost.set(laborCost.get() + materialCost.get() + baseCost.get());
            System.out.println("工程总造价计算完成: " + totalCost.get());
        }
    }

    public static void main(String[] args) {
        CostCalculator calculator = new CostCalculator();

        // 创建并启动计算人工成本的线程，设置为守护线程
        LaborCostCalculator laborThread = calculator.new LaborCostCalculator();
        laborThread.setDaemon(true);
        laborThread.start();

        // 创建并启动计算材料成本的线程，设置为守护线程
        MaterialCostCalculator materialThread = calculator.new MaterialCostCalculator();
        materialThread.setDaemon(true);
        materialThread.start();

        // 创建并启动计算工程基础造价的线程，设置为守护线程
        BaseCostCalculator baseThread = calculator.new BaseCostCalculator();
        baseThread.setDaemon(true);
        baseThread.start();

        // 创建并启动计算总造价的线程
        TotalCostCalculator totalThread = calculator.new TotalCostCalculator();
        totalThread.start();
    }
}